// pages/table/mall.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    MainCur: 0,
    VerticalNavTop: 0,
    list: [{
      'name': '方便面', 'id': 0, 'goodList': [{ 'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红烧肉", "price": 3, "num": 1, "singlePrice": 3 }, {
        'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红塔山", "price": 16, "num": 1, "singlePrice": 16
      }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极玉溪", "price": 22, "num": 1, "singlePrice": 22
        }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极玉溪", "price": 22, "num": 1, "singlePrice": 22
        }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极玉溪", "price": 22, "num": 1, "singlePrice": 22
        }]},
      {
        'name': '矿泉水', 'id': 1, 'goodList': [{ 'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红烧肉", "price": 3, "num": 1, "singlePrice": 3 }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红塔山", "price": 16, "num": 1, "singlePrice": 16
        }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极玉溪", "price": 22, "num": 1, "singlePrice": 22
        }]},
      {
        'name': '零食', 'id': 2, 'goodList': [{ 'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红烧肉", "price": 3, "num": 1, "singlePrice": 3 }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红塔山", "price": 16, "num": 1, "singlePrice": 16
        }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极玉溪", "price": 22, "num": 1, "singlePrice": 22
        }]},
      {
        'name': '烟', 'id': 3, 'goodList': [{ 'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红烧肉", "price": 3, "num": 1, "singlePrice": 3 }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红塔山", "price": 16, "num": 1, "singlePrice": 16
        }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极玉溪", "price": 22, "num": 1, "singlePrice": 22
        }] },
      {
        'name': '饮料', 'id': 4, 'goodList': [{ 'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红烧肉", "price": 3, "num": 1, "singlePrice": 3 }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红塔山", "price": 16, "num": 1, "singlePrice": 16
        }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极玉溪", "price": 22, "num": 1, "singlePrice": 22
        }] }, { 'name': '饮料', 'id': 5 },
      {
        'name': '饮料', 'id': 6, 'goodList': [{ 'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红烧肉", "price": 3, "num": 1, "singlePrice": 3 }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红塔山", "price": 16, "num": 1, "singlePrice": 16
        }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极玉溪", "price": 22, "num": 1, "singlePrice": 22
        }]},
      {
        'name': '饮料', 'id': 7, 'goodList': [{ 'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红烧肉", "price": 3, "num": 1, "singlePrice": 3 }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红塔山", "price": 16, "num": 1, "singlePrice": 16
        }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极玉溪", "price": 22, "num": 1, "singlePrice": 22
        }]},
      {
        'name': '饮料', 'id': 8, 'goodList': [{ 'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红烧肉", "price": 3, "num": 1, "singlePrice": 3 }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红塔山", "price": 16, "num": 1, "singlePrice": 16
        }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极玉溪", "price": 22, "num": 1, "singlePrice": 22
        }]},
      {
        'name': '饮料', 'id': 9, 'goodList': [{ 'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红烧肉", "price": 3, "num": 1, "singlePrice": 3 }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红塔山", "price": 16, "num": 1, "singlePrice": 16
        }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极玉溪", "price": 22, "num": 1, "singlePrice": 22
        }]},
      {
        'name': '饮料', 'id': 10, 'goodList': [{ 'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红烧肉", "price": 3, "num": 1, "singlePrice": 3 }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红塔山", "price": 16, "num": 1, "singlePrice": 16
        }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极玉溪", "price": 22, "num": 1, "singlePrice": 22
        }]},
      {
        'name': '饮料', 'id': 11, 'goodList': [{ 'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红烧肉", "price": 3, "num": 1, "singlePrice": 3 }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极红塔山", "price": 16, "num": 1, "singlePrice": 16
        }, {
          'url': 'https://ossweb-img.qq.com/images/lol/img/champion/Morgana.png', 'title': "究极玉溪", "price": 22, "num": 1, "singlePrice": 22
        }] }],
    goodList: [],
    load: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    // let list = [{}];
    // for (let i = 0; i < 26; i++) {
    //   list[i] = {};
    //   list[i].name = String.fromCharCode(65 + i);
    //   list[i].id = i;
    // }
    var list = this.data.list ;
    this.setData({
      // list: list,
      listCur: list[0]
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("onShow")
    let info = wx.getStorageSync('shopCar');
    if(info){
      let len = info.length;
      this.setData({
        len: len
      }) 
    }else{
      this.setData({
        len: 0
      }) 
    }
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      MainCur: e.currentTarget.dataset.id,
      VerticalNavTop: (e.currentTarget.dataset.id - 1) * 50
    })
  },
  VerticalMain(e) {
    let that = this;
    let list = this.data.list;
    let tabHeight = 0;
    if (this.data.load) {
      for (let i = 0; i < list.length; i++) {
        let view = wx.createSelectorQuery().select("#main-" + list[i].id);
        view.fields({
          size: true
        }, data => {
          list[i].top = tabHeight;
          tabHeight = tabHeight + data.height;
          list[i].bottom = tabHeight;
        }).exec();
      }
      that.setData({
        load: false,
        list: list
      })
    }
    let scrollTop = e.detail.scrollTop + 20;
    for (let i = 0; i < list.length; i++) {
      if (scrollTop > list[i].top && scrollTop < list[i].bottom) {
        that.setData({
          VerticalNavTop: (list[i].id - 1) * 50,
          TabCur: list[i].id
        })
        return false
      }
    }
  },
  trigger(name) {
    this.setData({ name, show: true });
    setTimeout(() => {
      this.setData({ show: false });
    }, 500);
  },
  addShopCar:function(e){
    /*先获取购物车缓存，查看是否有数据
     *如果有，获取数据，把现在的数据添加进去，再保存
     *如果没有，创建对象，把对象添加，再保存
     */
   this.trigger('fade-up');
    var data = e.currentTarget.dataset.item ;
    //1.获取缓存
    let info = wx.getStorageSync('shopCar');
    if (info){
      console.log("if");
      console.log(info);
      info.push(data);
      wx.setStorageSync('shopCar', info);
    }else{
      console.log("else");
      var obj = [];
      obj.push(data);
      wx.setStorageSync('shopCar', obj);
    }
    let infoData = wx.getStorageSync('shopCar');
    let len = infoData.length;
    this.setData({
      len:len
    })   
  },
  toShopCar:function(){
    wx.navigateTo({
      url: '/pages/mallMission/index',
    })
  }
})